package com.gjw.cor;

import com.gjw.planeWar.GameObject;
import com.gjw.planeWar.PropertyMgr;

import java.util.LinkedList;
import java.util.List;

/**
 * @author gjw19
 * @date 2019/5/19
 */
public class ColliderChain implements Collider {
    // 使用LinkedList可以节省空间
    private List<Collider> colliders = new LinkedList<>();

    private static final ColliderChain INSTANCE = new ColliderChain();

    public static ColliderChain getInstance() {
        return INSTANCE;
    }

    private ColliderChain() {
        List<String> list = PropertyMgr.getStringList("colliders");
        for (String s : list) {
            try {
                // 使用反射获得类
                colliders.add((Collider) Class.forName(s).getDeclaredConstructor().newInstance());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void add(Collider c) {
        colliders.add(c);
    }

    @Override
    public boolean collide(GameObject o1, GameObject o2) {
        for (int i = 0; i < colliders.size(); i++) {
            if (!colliders.get(i).collide(o1, o2)) {
                return false;
            }
        }
        return true;
    }
}
